//index.js
//获取应用实例
const app = getApp();
import {
  PATHS_ALIAS
} from '../../utils/api.js'

import {
  http
} from '../../utils/network.js'


Page({
  data: {
    actList: [],
    page: '1',
    pageSize: '10',
    totalPages: '',
    scrollHeight: '600rpx', //主体高度
    listStatus: 'all', //all 数据加载完了  loading 加载中   no 无状态

  },
  onLoad: function() {
    let _this = this;
    // 判断微信版本
    this.checkVersion();

    // 计算主体高度
    wx.getSystemInfo({
      success: function(res) {
        let windowHeight = res.windowHeight;
        let windowWidth = res.windowWidth;
        _this.setData({
          scrollHeight: windowHeight - (windowWidth / 750) * 0 + 'px'
        })
      }
    });

  },
  onShow: function () {
    this.getActList(true);
  },
  // 获取活动列表   init(true初次加载)
  getActList: function(init) {
    let _this = this;
    let page = init ? '1' : this.data.page;
    let params = {
      page: page, //	页数	string	
      pageSize: this.data.pageSize, //	每页大小	string	
    };
    this.setData({
      page,
      listStatus: 'loading'
    })
    http({
      path: PATHS_ALIAS.GET_GROUP_LIST,
      params: params,
      success: function(res) {
        //console.log(res);
        let actList = _this.data.actList;
        if (init) {
          actList = [];
        };
        let totalPages = res.response.totalPages;
        let groupActList = res.response.groupActList ? res.response.groupActList:[];
        groupActList.forEach(function(item, i) {
          groupActList[i].minGroupBuyPrice = item.groupBuyPrice.split('-')[0];
          groupActList[i].MinEntryFee = item.entryFee.split('-')[0];
        })
        actList = actList.concat(groupActList);
        _this.setData({
          actList,
          totalPages,
          listStatus: totalPages == page ? 'all' : 'no'
        });
      },
      fail: function(err) {

      }
    })
  },
  // 滚动到底部
  scrollToLower: function() {
    //console.log(1);
    let page = this.data.page;
    let totalPages = this.data.totalPages;

    // 数据加载完了
    if (page == totalPages) {
      return;
    } else {
      page = parseInt(page) + 1;
      this.setData({
        page
      })
      this.getActList();
    };

  },





  // 显示模态对话框 ———— 版本过低提示
  checkVersion: function() {
    let _this = this;
    wx.getSystemInfo({
      success: function(res) {
        // //console.log(res);
        let version = res.SDKVersion;
        version = version.replace(/\./g, '');
        if (parseInt(version) < 167) { //小于 1.6.7的版本
          wx.showModal({
            title: '提示',
            content: '微信版本过低，请前往升级微信',
            showCancel: false,
            success: function(res) {
              // //console.log(res)
            }
          })
          return;
        }
      }
    });
  },
  

})